/**
 * SimpleProductBean.java
 *
 * Created on 13 novembre 2007, 16:28
 */
package com.reimscopie.beans.entities;

import java.util.* ;

import javax.persistence.* ;

import com.reimscopie.* ;


/**
 * This entity represents a simple product, only for resale.
 */
@Entity
@NamedQueries({
  @NamedQuery(
    name  = "all-simple-products",
    query = "SELECT o FROM SimpleProductBean o ORDER BY name"
  ),
  @NamedQuery(
    name  = "all-valid-simple-products",
    query = "SELECT o FROM SimpleProductBean o WHERE valid=true ORDER BY name")
})

public class SimpleProductBean extends ProductBean<SimpleProduct> {
  
  // The price of this simple product
  private float price ;
  
  // The current stock of thi simple product
  private int stock ;
  
  
  /**
   * Create a new instance of <code>SimpleProductBean</code>
   * (default constructor).
   */
  public SimpleProductBean() {
  }
  
  
  /**
   * Create a new instance of <code>SimpleProductBean</code> with all
   * attributes.
   *
   * @param simpleProduct The valued SimpleProduct.
   */
  public SimpleProductBean(SimpleProduct simpleProduct) {
    
    if (simpleProduct.hasId())
      throw new IllegalArgumentException("simple-product has an image") ;
    
    setName(simpleProduct.getName()) ;
    setDescription(simpleProduct.getDescription()) ;
    setValid(simpleProduct.isValid()) ;
    setPicture(simpleProduct.getPicture()) ;
    
    this.price = simpleProduct.getPrice() ;
    this.stock = simpleProduct.getStock() ;
    
  }
  
  
  /**
   * Return the price of this simple product.
   *
   * @return The price of this simple product
   */
  public float getPrice() {
    return price ;
  }
  
  
  /**
   * Set the price of this simple product.
   *
   * @param price The price of this simple product.
   */
  public void setPrice(float price) {
    this.price = price ;
  }
  
  
  /**
   * Return the current stock of this simple product.
   *
   * @return The current stock of this simple product.
   */
  public int getStock() {
    return stock ;
  }
  
  
  /**
   * Set the current stock for this product.
   *
   * @param stock The current stock for this product.
   */
  public void setStock(int stock) {
    this.stock = stock ;
  }
  
  
  /**
   * Transform this entity bean into the associated value.
   * 
   * @return A new value instance.
   */
  @Override
  public SimpleProduct toValue() {
    
    Collection<CommentBean> collection = getComments() ;
    CommentBean[] beans = new CommentBean[collection.size()] ;
    collection.toArray(beans) ;
    
    Comment[] comments = new Comment[beans.length] ;
    for (int i=0;i<beans.length;i++)
      comments[i] = beans[i].toComment() ;
    
    return new SimpleProduct(
        getId(),
        getName(),
        getDescription(),
        getPicture(),
        getValid(),
        comments,
        getPrice(),
        getStock()
    ) ;
    
  }
  
}
